package net.arogarth.aws.android.manager.views.listener;

import net.arogarth.aws.android.lib.AmazonEc2;
import net.arogarth.aws.android.lib.models.Ec2Ami;
import net.arogarth.aws.android.lib.models.Ec2Instance;
import net.arogarth.aws.android.lib.models.Ec2Snapshot;
import net.arogarth.aws.android.manager.R;
import net.arogarth.aws.android.manager.views.Ec2AmiList;
import net.arogarth.aws.android.manager.views.Ec2EbsList;
import net.arogarth.aws.android.manager.views.Ec2InstanceList;
import net.arogarth.aws.android.manager.views.Ec2SnapshotList;
import net.arogarth.aws.android.manager.views.LaunchInstanceFromAmi;
import net.arogarth.aws.android.manager.views.RegisterAsAmi;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;

public class ListClickListener implements AdapterView.OnItemLongClickListener,
		AdapterView.OnItemClickListener {

	BaseAdapter adapter;
	Context context;

	public boolean onItemLongClick(AdapterView<?> adapter, View item,
			int position, long arg3) {
		this.adapter = (BaseAdapter) adapter.getAdapter();
		this.context = item.getContext();

		if (item.getContext().getClass().equals(Ec2EbsList.class)) {
//			this.onEbsItemClick(position);
		} else if (item.getContext().getClass().equals(Ec2InstanceList.class)) {
			this.onEc2InstanceClick(position);
		} else if (item.getContext().getClass().equals(Ec2SnapshotList.class)) {
			this.onEbsSnapshotLongClick(position);
		} else if (item.getContext().getClass().equals(Ec2AmiList.class)) {
			this.onAmiLongClick(position);
		}

		return true;
	}

	public void onItemClick(AdapterView<?> adapter, View item, int position,
			long arg3) {
		// TODO Auto-generated method stub

	}

	private boolean onEbsSnapshotLongClick(int position) {
		final Ec2Snapshot snapshot = (Ec2Snapshot) this.adapter
				.getItem(position);

		CharSequence[] items = { "Register as AMI", "Create Volume", "Delete", };

		AlertDialog.Builder builder = new AlertDialog.Builder(this.context);
		builder.setTitle(snapshot.getSnapshotId());
		builder.setItems(items, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				switch (which) {
				case 0: // Register as AMI
					Intent intent = new Intent(context, RegisterAsAmi.class);
					intent.putExtra("snapshotId", snapshot.getSnapshotId());
					context.startActivity(intent);
					break;
				case 1: // Create Volume
					break;
				case 2: // Delete
					break;
				}
			}
		});

		builder.create().show();

		return true;
	}

	private boolean onAmiLongClick(int position) {
		final Ec2Ami image = (Ec2Ami) this.adapter.getItem(position);

		CharSequence[] items = { "Launch Instance", "Deregister AMI" };

		AlertDialog.Builder builder = new AlertDialog.Builder(this.context);
		builder.setTitle(image.getImageId());
		builder.setItems(items, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				switch (which) {
				case 0: // Launch Instance
					context.startActivity(new Intent(context,
							LaunchInstanceFromAmi.class));
					break;
				case 1: // Deregister AMI
					new AmazonEc2().deregisterImage(image.getImageId());
					((Activity) context).finish();
					break;
				}
			}
		});

		builder.create().show();

		return true;
	}

	protected boolean onEc2InstanceClick(int position) {
		final Ec2Instance instance = (Ec2Instance) this.adapter
				.getItem(position);

		AlertDialog.Builder builder = new AlertDialog.Builder(this.context);
		builder.setTitle(instance.getInstanceId());

		final ArrayAdapter<CharSequence> adapter = ArrayAdapter
				.createFromResource(this.context, R.array.ec2Options,
						android.R.layout.select_dialog_item);

		builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				try {
					switch (which) {
					// case 0: // Connect
					// // new
					// // AmazonEc2().startInstance(instance.getInstanceId());
					// break;
					// case 1: // Create Snapshot
					// // new
					// // AmazonEc2().startInstance(instance.getInstanceId());
					// break;
					// case 2: // Launch More Like This
					// // new
					// // AmazonEc2().startInstance(instance.getInstanceId());
					// break;
					case 0: // Start
						new AmazonEc2()
								.startInstance(instance);
						break;
					case 1: // Stop
						new AmazonEc2()
								.stopInstance(instance);
						break;
					case 2: // Terminate
						new AmazonEc2()
								.terminateInstance(instance);
						break;
					}
				} catch (Exception e) {
				}
				adapter.notifyDataSetChanged();
			}
		});

		builder.create().show();

		return true;
	}

//	private boolean onEbsItemClick(int position) {
//		final Ec2Ebs ebsVolume = (Ec2Ebs) this.adapter.getItem(position);
//
//		AlertDialog.Builder builder = new AlertDialog.Builder(this.context);
//
//		if (ebsVolume.getStatus().equalsIgnoreCase("available")) {
//			CharSequence[] options = { "Attach", "Create Snapshot",
//					"Delete Volume" };
//
//			builder.setItems(options, new DialogInterface.OnClickListener() {
//				public void onClick(DialogInterface dialog, final int which) {
//					switch (which) {
//					case 0:
//						break;
//					case 1:
//
//						break;
//					case 3:
//						break;
//					}
//				}
//			});
//		} else {
//			CharSequence[] options = { "Detach", "Create Snapshot", };
//
//			builder.setItems(options, new DialogInterface.OnClickListener() {
//				public void onClick(DialogInterface dialog, final int which) {
//					switch (which) {
//					case 0:
//						break;
//					case 1:
//						try {
//							new AmazonEc2(AwsManager.getCurrentAwsAccount()
//									.getAccessKey(), AwsManager
//									.getCurrentAwsAccount().getSecretKey())
//									.createSnapshot(ebsVolume.getVolumeId(),
//											"TEST");
//						} catch (AwsRequestException e) {
//							e.printStackTrace();
//						}
//
//						break;
//					}
//				}
//			});
//		}
//
//		builder.create().show();
//
//		return true;
//	}

}
